package com.me {
	import com.me.components.Grid45;
	import com.me.math.MMath;

	import flash.events.MouseEvent;
	import flash.geom.Point;

	import mx.core.FlexGlobals;

	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 下午04:24:47
	 */
	public class GridLayerT extends SpriteVisualElement {

		private var _gridHeight:int = 32;

		private var _gridWidth:int = _gridHeight * 2;

		private var _gridStatus:Array = [];

		private var _cols:int = 0;

		private var _rows:int = 0;

		public function GridLayerT(w:int, h:int, gw:int = 64, gh:int = 32) {
			super();
			width = w;
			height = h;
			_gridWidth = gw;
			_gridHeight = gh;
			_rows = Math.ceil(height / _gridHeight);
			_cols = Math.ceil(width / _gridWidth);
			for (var row:int = 0; row < _rows * 2; row++) {
				var detail:Array = [];
				for (var col:int = 0; col < _cols; col++) {
					detail.push(0);
				}
				_gridStatus.push(detail);
			}
			drawGrid();
//			addEventListener(MouseEvent.MOUSE_MOVE, moveMoveHandler);
		}

		private function moveMoveHandler(e:MouseEvent):void {
			var clickcol:int = Math.floor(e.localX / _gridWidth);
			var clickrow:int = Math.floor(e.localY / _gridHeight);
			FlexGlobals.topLevelApplication.status = "X:" + e.localX + ".Y:" + e.localY + "/行:" + clickrow + ".列:" + clickcol;
		}


		private var lineColor:uint = 0xffffff;

		private var lineAlpha:Number = .5;

		private function drawGrid():void {
			for (var row:int = 0; row < _gridStatus.length; row++) {
				for (var col:int = 0; col < _gridStatus[row].length; col++) {
					var g:Grid45 = new Grid45(_gridWidth, _gridHeight, clickGridHandler);
					g.row = row;
					g.col = col;
					if (row % 2 == 0) { // 偶数
						g.x = col * g.width;
						g.y = row * g.height / 2;
					} else { // 奇数
						g.x = g.width / 2 + col * g.width;
						g.y = row * g.height / 2 //g.height / 2 + (row - 1) * g.height;
					}
					addChild(g);
				}
			}
		}

		private function clickGridHandler(g:Object):void {
			_gridStatus[g.row][g.col] = g.walk ? 1 : 0;
		}

		public function clear():void {
			graphics.clear();
		}

		public function get gridStatus():Array {
			return _gridStatus;
		}

		public function get cols():int {
			return _cols;
		}

		public function get rows():int {
			return _rows;
		}

		public function get gridHeight():int {
			return _gridHeight;
		}

		public function get gridWidth():int {
			return _gridWidth;
		}


	}
}
